package com.ybwh.springboot2.controller;


import com.ybwh.springboot2.common.RespData;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 调整线上日志级别的接口，用于线上问题定位
 * </p>
 *
 * @author fanbeibei
 * @version V1.0
 * @since 2019/7/3 10:50
 */
@Api(tags = "")
@RestController
@Slf4j
@RequestMapping("/")
public class LogController {


    @GetMapping("/log")
    public RespData<String> log(@RequestParam("packageName") String packageName) {
        System.out.println("----------------------------------------------------------");
        log.error("error -- hello");
        log.warn("warn -- hello");
        log.info("info -- hello");
        log.debug("debug -- hello");
        System.out.println("----------------------------------------------------------");


        LoggerContext loggerContext = LoggerContext.getContext(false);
        return RespData.successResp(loggerContext.getLogger(packageName).getLevel().toString());
    }

    @GetMapping("/changeLog")
    public RespData<String> changeLog( @RequestParam("packageName") String packageName,
                                       @RequestParam("logLevel") String logLevel) {
        try {
            LoggerContext loggerContext = LoggerContext.getContext(false);
            loggerContext.getLogger(packageName).setLevel(Level.valueOf(logLevel));
        } catch (Exception e) {
            log.error("动态修改日志级别出错，原因：" + e.getMessage(), e);
            return RespData.failedResp(ExceptionUtils.getStackTrace(e));
        }
        return RespData.successResp(null);
    }

    /**
     * LOG4j2动态修改包名的日志级别
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/levelb",method = RequestMethod.GET)
    public String updateLogbackLevelj( @RequestParam(value="level") String level) throws Exception {
        String msg = "success";
        level = level.toLowerCase();
        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        Configuration config = ctx.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig("com.ybwh.springboot2");
        switch (level) {
            case "trace":
                loggerConfig.setLevel(Level.TRACE);
                break;
            case "debug":
                loggerConfig.setLevel(Level.DEBUG);
                break;
            case "info":
                loggerConfig.setLevel(Level.INFO);
                break;
            case "warn":
                loggerConfig.setLevel(Level.WARN);
                break;
            case "error":
                loggerConfig.setLevel(Level.ERROR);
                break;
            default:
                msg = "日志级别修改失败！";
                break;
        }
        ctx.updateLoggers();
        return msg;
    }

}
